package com.springboot.base.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.base.common.AdminPermission;
import com.springboot.base.common.BusinessException;
import com.springboot.base.common.CommonUtil;
import com.springboot.base.common.EmBusinessError;
import com.springboot.base.model.Category;
import com.springboot.base.model.Shop;
import com.springboot.base.request.CategoryCreateReq;
import com.springboot.base.request.PageQuery;
import com.springboot.base.request.ShopCreateReq;
import com.springboot.base.service.ICategoryService;
import com.springboot.base.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

@Controller("/admin/shop")
@RequestMapping("/admin/shop")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 品类列表
     */
    @RequestMapping("/index")
    @AdminPermission
    public ModelAndView index(PageQuery pageQuery){
        Page<Shop> page = new Page(pageQuery.getPage(),pageQuery.getSize());
        IPage<Shop>  shopList = shopService.qryListByPage(page);
        ModelAndView modelAndView = new ModelAndView("/admin/shop/index.html");
        modelAndView.addObject("data",shopList);
        modelAndView.addObject("CONTROLLER_NAME","shop");
        modelAndView.addObject("ACTION_NAME","index");
        return modelAndView;
    }

    @RequestMapping("/createpage")
    @AdminPermission
    public ModelAndView createPage(){
        ModelAndView modelAndView = new ModelAndView("/admin/shop/create.html");
        modelAndView.addObject("CONTROLLER_NAME","shop");
        modelAndView.addObject("ACTION_NAME","create");
        return modelAndView;
    }

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    @AdminPermission
    public String create(@Valid ShopCreateReq shopCreateReq, BindingResult bindingResult) throws BusinessException {
        if(bindingResult.hasErrors()){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, CommonUtil.processErrorString(bindingResult));
        }

        Shop shop = new Shop();
        shop.setIconUrl(shopCreateReq.getIconUrl());
        shop.setAddress(shopCreateReq.getAddress());
        shop.setCategoryId(shopCreateReq.getCategoryId());
        shop.setEndTime(shopCreateReq.getEndTime());
        shop.setStartTime(shopCreateReq.getStartTime());
        shop.setLongitude(shopCreateReq.getLongitude());
        shop.setLatitude(shopCreateReq.getLatitude());
        shop.setName(shopCreateReq.getName());
        shop.setPricePerMan(shopCreateReq.getPricePerMan());
        shop.setSellerId(shopCreateReq.getSellerId());

        shopService.create(shop);

        return "redirect:/admin/shop/index";
    }


}
